﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using VswService_V7.Utility;

namespace VswService_V7.DataBase.Tables.L3INTERFACE
{
    public class L3INTF_TASK_DELETE
    {
        #region field
        public static readonly string tableName = "L3INTF_TASK_DELETE";
        public static readonly string fd_eventNo = "EVENT_NO";
        public static readonly string fd_taskNo = "TASK_NO";
        public static readonly string fd_updateTime = "UPDATE_TIME";
        #endregion
        /// <summary>
        /// L3 deletes one task on L2
        /// </summary>
        /// <param name="event_id"></param>
        public static void DeleteTask(string event_id)
        {
            try
            {
                string sqlTask = string.Format("select * from {0} where {1}={2}", tableName, fd_eventNo, event_id);
                DataTable taskTable = OracleHelper.Exct_QueryTable(sqlTask);
                if (taskTable == null)
                {
                    string l3infcEventsql = string.Format("update {0} set {1}='{2}',{3}='{4}' where {5}={6}",
                        L3INTF_EVENT.tableName, L3INTF_EVENT.fd_eventStatus, "-1", L3INTF_EVENT.fd_errorInfo, "TASK_NO not exist",
                        L3INTF_EVENT.fd_eventNo, taskTable.Rows[0][TASK_INFO.fd_eventNo].ToString());
                    OracleHelper.Exct_NonQuery(l3infcEventsql);
                    return;
                }
                if (taskTable.Rows.Count != 1)
                {
                    string l3infcEventsql = string.Format("update {0} set {1}='{2}',{3}='{4}' where {5}={6}",
                        L3INTF_EVENT.tableName, L3INTF_EVENT.fd_eventStatus, "-1", L3INTF_EVENT.fd_errorInfo, "not unique TASK_NO",
                        L3INTF_EVENT.fd_eventNo, taskTable.Rows[0][TASK_INFO.fd_eventNo].ToString());
                    OracleHelper.Exct_NonQuery(l3infcEventsql);
                    return;
                }
                if (taskTable != null && taskTable.Rows.Count == 1)
                {
                    ExecuteDeleteTask(taskTable);
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("L3INTF_TASK_DELETE.DeleteTask", ex);
                throw (ex);
            }
        }
        private static void ExecuteDeleteTask(DataTable taskTable)
        {
            try
            {
                string sqlTask = string.Format("delete from {0} where {1}='{2}'",
                   TASK_INFO.tableName, TASK_INFO.fd_taskNo, taskTable.Rows[0][TASK_INFO.fd_taskNo].ToString());
                string sqlStrip = string.Format("delete from {0} where {1}='{2}'",
                    STRIP_INFO.tableName, STRIP_INFO.fd_taskNo, taskTable.Rows[0][TASK_INFO.fd_taskNo].ToString());
                string l3infcEventsql = string.Format("update {0} set {1}={2} where {3}={4}", L3INTF_EVENT.tableName, L3INTF_EVENT.fd_eventStatus, "2", L3INTF_EVENT.fd_eventNo, taskTable.Rows[0][TASK_INFO.fd_eventNo].ToString());
                List<string> sqlList = new List<string>();
                sqlList.Add(sqlTask);
                sqlList.Add(sqlStrip);
                sqlList.Add(l3infcEventsql);
                OracleHelper.ExecuteSqlTran(sqlList);
                LogHelper.WriteLog("L3INTF_TASK_DELETE.ExecuteDeleteTask Success!");
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("L3INTF_TASK_DELETE.ExecuteDeleteTask", ex);
                throw ex;
            }
        }
    }
}
